package com.dashan.backend.datastore;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.dashan.backend.XmlDeserializable;
import com.dashan.backend.XmlSerializable;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class DsHighSchool implements XmlSerializable, XmlDeserializable {
    @SuppressWarnings("unused")
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;
    
    @Persistent
    private String name = "";
    
    @Persistent
    private String enrollmentYear = "";

    public DsHighSchool() {
        
    }
    
    public DsHighSchool(String name, String enrollmentYear) {
        this.name = name;
        this.enrollmentYear = enrollmentYear;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEnrollmentYear() {
        return enrollmentYear;
    }

    public void setEnrollmentYear(String enrollmentYear) {
        this.enrollmentYear = enrollmentYear;
    }
    
    @Override
    public Element getXmlElement(Document xmlDoc) {
        Element root = xmlDoc.createElement("highschool");
        Element ele = null;
        
        ele = xmlDoc.createElement("name");
        ele.appendChild(xmlDoc.createTextNode(name));
        root.appendChild(ele);

        ele = xmlDoc.createElement("enrollmentYear");
        ele.appendChild(xmlDoc.createTextNode(enrollmentYear));
        root.appendChild(ele);
        
        return root;
    }
    
    @Override
    public void fromXmlElement(Element ele) {
        NodeList e = ele.getElementsByTagName("name");
        if (e.getLength() != 0 && e.item(0).getFirstChild() != null) {
            name = e.item(0).getFirstChild().getNodeValue();;
        }
        e = ele.getElementsByTagName("enrollmentYear");
        if (e.getLength() != 0 && e.item(0).getFirstChild() != null) {
            enrollmentYear = e.item(0).getFirstChild().getNodeValue();;
        }
    }
    
    @Override
    public String toString() {
        return String.format("%s(%s)", name, enrollmentYear);
    }
}
